package com.borya.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Base64;
import java.util.List;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.apache.log4j.Logger;

import com.sun.image.codec.jpeg.JPEGCodec;

//import com.sun.image.codec.jpeg.JPEGCodec;
//import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageUtils {

	private static Logger log = Logger.getLogger(ImageUtils.class);
	
	// 读入文件
	private static File createFile(String srcImgFileName){
		return new File(srcImgFileName);
	}
	
	private static BufferedImage createBufferedImage(File file)throws IOException{
		return ImageIO.read(file);
	}

	public static BufferedImage createBufferedImage(String srcImgFileName)throws IOException{
		return createBufferedImage(createFile(srcImgFileName));
	}
	
	/**		
	 * 缩放图片
	 * @param srcImgFileName 	源文件绝对路径(新文件件路径)
	 * @param newWidth			新文件宽度
	 * @param newHeight			新文件高度
	 * @throws IOException
	 */
	public static void narrowImage(String srcImgFileName,int newWidth,int newHeight) throws IOException {
		narrowImage(srcImgFileName,srcImgFileName,newWidth,newHeight) ;
	}
	/**
	 * 对图片进行缩放
	 * 
	 * @param srcImgFileName 绝对路径(文件路径 +文件名)
	 * @throws IOException
	 */
	public static boolean narrowImage(String srcImgFileName,String dirImgFilePath,int width) throws IOException {
		File file = createFile(srcImgFileName);
		BufferedImage src = createBufferedImage(file);
//		int width = src.getWidth();
//		int height = src.getHeight();

		// 边长缩小为二分之一
		BufferedImage tag = new BufferedImage(width , width,
				BufferedImage.TYPE_INT_RGB);
		// 绘制缩小后的图
		tag.getGraphics().drawImage(src, 0, 0, width , width, null);

		return write(tag, dirImgFilePath);
	}
	
	public static boolean write(BufferedImage img,String dstFilePath){
		return write(img,new File(dstFilePath));
	}
	
	public static boolean writeImg(String dir, String imgName, String base64Str) throws IOException{
		File file = new File(dir);
		if (!file.exists()) {
			file.mkdirs();
		}
		byte[] bytes = Base64.getDecoder().decode(base64Str);
		try(ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);){
			bytes = null;
			String fileName = dir+ imgName;
			BufferedImage bufImage = ImageIO.read(byteArrayInputStream);

			boolean ok = write(bufImage, fileName);
			log.debug("保存图片：" + imgName + (ok ? "成功" : "失败"));
			return ok;
		}
	}

    public static boolean writeImg(String dir, String imgName, byte[] imgs) throws IOException{
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdirs();
        }
        try(ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imgs);){
            byte[] bytes = null;
            String fileName = dir+ imgName;
            BufferedImage bufImage = ImageIO.read(byteArrayInputStream);
            boolean ok = write(bufImage, fileName);
            log.debug("保存图片：" + imgName + (ok ? "成功" : "失败"));
            return ok;
        }
    }
	
	public static boolean write(BufferedImage img,File newFile){
		try(FileOutputStream out = new FileOutputStream(newFile);) {
			//XXX ImageIO.write 生成的图片文件比较大
//			String formatName = newFile.getName().substring(newFile.getName().lastIndexOf(".")+1);
//			boolean result = ImageIO.write(img, formatName, newFile);
//			return result;
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			encoder.encode(img);
			out.close();
			return true;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return false;
		}
	}
	
	public static void narrowImage(String srcImgFileName,String dirImgFilePath,int newWidth,int newHeight) throws IOException {
		File file = createFile(srcImgFileName);
		BufferedImage src = createBufferedImage(file);
		BufferedImage tag = new BufferedImage(newWidth,newHeight,
				BufferedImage.TYPE_INT_RGB);
		// 绘制缩小后的图
		tag.getGraphics().drawImage(src, 0, 0,newWidth,newHeight, null);
		write(tag, dirImgFilePath);
	}
	/**
	 * 扩大文件
	 * @param srcImgFileName 源文件路径
	 * @param newWidth	新文件的宽度
	 * @param newHeight 新文件的高度
	 * @throws IOException
	 */
	public static void enlarge(String srcImgFileName,int newWidth,int newHeight)throws IOException{
		enlarge(srcImgFileName,srcImgFileName,newWidth,newHeight);
	}	
	
	/**
	 * 对图片进行扩大
	 * @param srcImgFileName 源文件的路径
	 * @param dirImagFilePath 新文件的路径
	 * @param newWidth 新文件的宽度
	 * @param newHeight 新文件的高度
	 * @throws IOException
	 */
	public static void enlarge(String srcImgFileName,String dirImagFilePath,int newWidth,int newHeight)throws IOException{
		BufferedImage src = createBufferedImage(srcImgFileName);
		
		// 边长扩大
		BufferedImage tag = new BufferedImage(newWidth, newHeight,BufferedImage.TYPE_INT_RGB);
		tag.getGraphics().drawImage(src, 0, 0, newWidth , newHeight , null);
		write(tag, dirImagFilePath);
	}
	/**
	 * 裁剪图片
	 * @param srcImageFile      源文件路径
	 * @param dirImageFilePath  新文件夹的路径
	 * @param x					裁剪的坐标x
	 * @param y					裁剪的坐标y
	 * @param newWidth			裁剪的宽度
	 * @param newHeight			裁剪的高度
	 * @throws IOException
	 */
	public static void cut(String srcImageFile,String dirImageFilePath,int x,int y,int newWidth,int newHeight) throws IOException {
		// 读取源图像
		BufferedImage src = ImageIO.read(new File(srcImageFile));
		// 四个参数分别为图像起点坐标和宽高
		ImageFilter cropFilter = new CropImageFilter(x,y,newWidth,newHeight);
		Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(src.getSource(), cropFilter));
		BufferedImage tag = new BufferedImage(newWidth,newHeight,BufferedImage.TYPE_INT_RGB);
		Graphics g = tag.getGraphics();
//		g.setColor(Color.white);
//		g.drawImage(img, 0, 0, null); // 绘制小图
		g.fillRect(0, 0, newWidth, newHeight);  
		g.drawImage(img, 0, 0, newWidth, newHeight, null);
		g.dispose();
		// 输出为文件
		write(tag, dirImageFilePath);
	}
	/**
	 * 生成图片文字
	 * @param imgSavePath  图片保存的路径
	 * @param fileName     文件名
	 * @param width        图片宽度
	 * @param height       图片高度
	 * @param str          背景文字
	 */
	public synchronized boolean createImg(String imgSavePath,String fileName,String str,int width,int height){   
        try {
			final String filePath = imgSavePath + fileName;//"c:/image.jpg"
//			File file = new File(filePath);
//			Font font = new Font("Serif", Font.BOLD, 10);
			Font font = new Font("微软雅黑", Font.BOLD, 14);
			BufferedImage bi = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
			Graphics2D g2 = (Graphics2D) bi.getGraphics();
			g2.setBackground(Color.WHITE);
			g2.clearRect(0, 0, width, height);
			g2.setPaint(Color.BLACK);
			FontRenderContext context = g2.getFontRenderContext();
			Rectangle2D bounds = font.getStringBounds(str, context);
			double x = (width - bounds.getWidth()) / 2;
			double y = (height - bounds.getHeight()) / 2;
			double ascent = -bounds.getY();
			double baseY = y + ascent;
			g2.drawString(str, (int) x, (int) baseY);
//			ImageIO.write(bi, "jpg", file);
			return write(bi, filePath);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return false;
		}   
    }   
	
	public synchronized static void createImg(String imgSavePath,String fileName,List<String> listStr,int width){   
		final String filePath = imgSavePath + fileName;//"c:/image.jpg"
		File file = new File(filePath);
		final int height = 3 * (15+5);
		BufferedImage bi = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
		Graphics2D g2 = (Graphics2D) bi.getGraphics();
		g2.setBackground(Color.WHITE);
		g2.clearRect(0, 0, width, height);
		g2.setPaint(Color.DARK_GRAY);
		g2.setFont(new Font("宋体", Font.BOLD, 14));
		int y = 15;
		for(String str : listStr){
			g2.drawString(str, width/4, y);
			y += 20;
		}
		
		g2.dispose();  
		write(bi, file);
	}   
	
	/** 
     * 添加图片水印 
     * @param targetImg 目标图片路径，如：C://myPictrue//1.jpg 
     * @param waterImg  水印图片路径，如：C://myPictrue//logo.png 
     * @param x 水印图片距离目标图片左侧的偏移量，如果x<0, 则在正中间 
     * @param y 水印图片距离目标图片上侧的偏移量，如果y<0, 则在正中间 
     * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明，1.0为完全不透明) 
     */  
    public static void pressImage(String targetImg, String waterImg, int x, int y, float alpha) {  
        try {  
        	Image image = ImageIO.read(new File(targetImg));  
            int width = image.getWidth(null);  
            int height = image.getHeight(null);  
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
            Graphics2D g = bufferedImage.createGraphics();  
            g.drawImage(image, 0, 0, width, height, null);  
          
            Image waterImage = ImageIO.read(new File(waterImg));    // 水印文件  
            int width_1 = waterImage.getWidth(null);  
            int height_1 = waterImage.getHeight(null);  
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));  
              
            int widthDiff = width - width_1;  
            int heightDiff = height - height_1;  
            if(x < 0){  
                x = widthDiff / 2;  
            }else if(x > widthDiff){  
                x = widthDiff;  
            }  
            if(y < 0){  
                y = heightDiff / 2;  
            }else if(y > heightDiff){  
                y = heightDiff;  
            }  
            g.drawImage(waterImage, x, y, width_1, height_1, null); // 水印文件结束  
            g.dispose();  
//            ImageIO.write(bufferedImage, "jpg", file);  
            write(bufferedImage, targetImg);
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }
	/**
     * 给图片添加图片水印
     * @param pressImg 水印图片
     * @param srcImageFile 源图像地址
     * @param destImageFile 目标图像地址
     * @param x 修正值。 默认在中间
     * @param y 修正值。 默认在中间
     * @param alpha 透明度：alpha 必须是范围 [0.0, 1.0] 之内（包含边界值）的一个浮点数字
     */
    public final static void pressImage(String pressImg, String srcImageFile,String destImageFile,
            int x, int y, float alpha) {
        try {
        	Image src = ImageIO.read(new File(srcImageFile));
            int wideth = src.getWidth(null);
            int height = src.getHeight(null);
            BufferedImage image = new BufferedImage(wideth, height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image.createGraphics();
            g.drawImage(src, 0, 0, wideth, height, null);
            // 水印文件
            Image src_biao = ImageIO.read(new File(pressImg));
            int wideth_biao = src_biao.getWidth(null);
            int height_biao = src_biao.getHeight(null);
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));
            g.drawImage(src_biao, (wideth - wideth_biao) / 2,
                    (height - height_biao) / 2, wideth_biao, height_biao, null);
            // 水印文件结束
            g.dispose();
//            ImageIO.write((BufferedImage) image,  "JPEG", new File(destImageFile));
            write(image, destImageFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static BufferedImage bufImg(String imageFilePath)throws IOException{
    	return ImageIO.read(new File(imageFilePath));
    }
    
    /**  
     * 截取一个图像的中央区域  
     * @param filePath 图像File
     * @param w 需要截图的宽度  
     * @param h 需要截图的高度  
     * @return 返回一个  
     * @throws IOException  
     */  
    public static void cutImageCenter(String filePath, int w, int h) throws IOException {   
    	File image = new File(filePath); 
    	// 判断参数是否合法   
        if (null == image || 0 == w || 0 == h) {   
            new Exception ("哎呀，截图出错！！！");   
        }   
        // 用ImageIO读取字节流   
        BufferedImage bufferedImage = ImageIO.read(image);   
        // 返回源图片的宽度。   
        int srcW = bufferedImage.getWidth();   
        // 返回源图片的高度。   
        int srcH = bufferedImage.getHeight();   
        int x = 0, y = 0;   
        // 使截图区域居中   
        x = srcW / 2 - w / 2;   
        y = srcH / 2 - h / 2;   
        srcW = srcW / 2 + w / 2;   
        srcH = srcH / 2 + h / 2;   
        // 生成图片   
        BufferedImage distin = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);   
        Graphics g = distin.getGraphics();   
        g.drawImage(bufferedImage, 0, 0, w, h, x, y, srcW, srcH, null);   
//        String suffix = filePath.substring(filePath.lastIndexOf(".")+1);
//        ImageIO.write(distin, suffix, new File(filePath));
        write(distin, filePath);
    }
    
    /***
     * 图片拼接(横着拼)
     * @param file1
     * @param file2
     * @throws IOException
     */  
    public static boolean mergeImageHorizontally(File file1, File file2,String destPathAndName) throws IOException {        
        BufferedImage image1 = ImageIO.read(file1);  
        BufferedImage image2 = ImageIO.read(file2);  
  
        BufferedImage combined = new BufferedImage(image1.getWidth() * 2, image1.getHeight(), BufferedImage.TYPE_INT_RGB);  
  
        // paint both images, preserving the alpha channels  
        Graphics g = combined.getGraphics();  
        g.drawImage(image1, 0, 0, null);  
        g.drawImage(image2, image1.getWidth(), 0, null);  
          
        // Save as new image  
//        ImageIO.write(combined, "JPG", new File(destPathAndName));  
        return  write(combined, destPathAndName);
    } 

    /**
     * 图片拼接(竖着拼)
     * @param file1
     * @param file2
     * @param destPathAndName
     * @return
     * @throws Exception
     */
    public static boolean mergeImageVertically(File file1,File file2,String destPathAndName) throws Exception {  
        Image src1 = javax.imageio.ImageIO.read(file1);  
        return mergeImageVertically(src1,file2,destPathAndName);
    }
    
    /**
     * 图片拼接(竖着拼)
     * @param src1
     * @param file2
     * @param destPathAndName
     * @return
     * @throws Exception
     */
    public static boolean mergeImageVertically(Image src1,File file2,String destPathAndName) throws Exception {  
    	Image src2 = javax.imageio.ImageIO.read(file2);  
        return mergeImageVertically(src1, src2,destPathAndName);
    }  
    
    /**
     * 图片拼接(竖着拼)
     * @param src1
     * @param src2
     * @param destPathAndName
     * @return
     * @throws Exception
     */
    public static boolean mergeImageVertically(Image src1,Image src2,String destPathAndName) throws Exception {  
    	//获取图片的宽度/高度  
        final int width1 = src1.getWidth(null);  
        final int height1 = src1.getHeight(null);  
        final int width2 = src2.getWidth(null);
        final int height2 = src2.getHeight(null);  
        
        final int newWidth = width1>width2 ? width1:width2;
        //构造一个类型为预定义图像类型之一的 BufferedImage。 宽度为第一只的宽度，高度为各个图片高度之和  
        BufferedImage tag = new BufferedImage(newWidth, height1 + height2, BufferedImage.TYPE_INT_RGB);  
        //绘制合成图像  
        Graphics g = tag.createGraphics();  
        g.drawImage(src1, 0, 0, width1, height1, null);  
        g.drawImage(src2, 0, height1, width1 , height2, null);  
        // 释放此图形的上下文以及它使用的所有系统资源。  
        g.dispose();  
        
        return write(tag, destPathAndName);
    }  
    
    /**
	 * 文字水印
	 * @param image
	 * @param destPathAndName : d://new_image.png
	 * @return
	 */
	public static boolean waterMarkBySingleText(File image,String destPathAndName,String waterText,int extraY){
		return 	waterMarkBySingleText(image,new File(destPathAndName),waterText,extraY);
	}
	
    /**
	 * 文字水印
	 * @param image
	 * @param destPathAndName : d://new_image.png
	 * @return
	 */
	public static boolean waterMarkBySingleText(File image,File destPathAndName,String waterText,int extraY){
		//水印文字
		//final String waterText = "测试水印";
		//字体
		final String fontName = "微软雅黑";//"华文行楷";
		//字体样式
		final int fontStyle = Font.PLAIN + Font.ITALIC;
		//字体大小 单位像素
		final int fontSize = 30;
		//文字颜色
		final Color color = Color.RED;
		//文字水印透明度 30%
		final float ALPHA = 0.3F;
		//绘制位置横坐标
		//final int X = 20;
		//绘制位置纵坐标
		//final int Y = 120;
		
		try {
			//通过ImageIO获取图像文件的像素大小 即宽/高
			Image imageTemp = ImageIO.read(image);
			final int width = imageTemp.getWidth(null);
			final int height = imageTemp.getHeight(null);
			//1.创建缓存图片对象--BufferedImage
			BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			//2.创建java绘图工具对象--Graphics2D
			Graphics2D graphics2D = bufferedImage.createGraphics();
			//3.使用绘图工具对象将原图绘制到缓存图片对象
			/**
			 * img the specified image to be drawn. This method does nothing if img is null.
			 * x the x coordinate.
			 * y the y coordinate.
			 * width the width of the rectangle.
			 * height the height of the rectangle.
			 * observer object to be notified as more of the image is converted.
			 */
			graphics2D.drawImage(imageTemp, 0, 0, width,height,null);
			
			//设置水印文字 字体 样式 大小
			graphics2D.setFont(new Font(fontName, fontStyle,fontSize));// (int)(width / getTextLength(waterText)*1.5)));
			///设置水印文字颜色
			graphics2D.setColor(color);
			
			//设置水印透明度 0.3
			graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));
			//4.使用绘图工具将水印（文字或图片）绘制到缓存图片对象
			graphics2D.drawString(waterText, 0, height/4 * 3 + extraY);
			//释放工具
			graphics2D.dispose();
			
			return write(bufferedImage, destPathAndName);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return false;
		}
	}

	/**  
     * 添加图片水印  
     * @param targetImg 目标图片路径，如：C://myPictrue//1.jpg  
     * @param waterImg  水印图片路径，如：C://myPictrue//logo.png  
     * @param x 水印图片距离目标图片左侧的偏移量，如果x<0, 则在正中间  
     * @param y 水印图片距离目标图片上侧的偏移量，如果y<0, 则在正中间  
     * @param alpha 透明度(0.0 -- 1.0, 0.0为完全透明，1.0为完全不透明)  
     * @throws Exception  
     */  
    public static boolean waterMarkByImg(String srcImg,String waterImg,String targetImg,int x,int y,float alpha) throws Exception{  
        try {  
        	Image image = ImageIO.read(new File(srcImg));  
            final int width = image.getWidth(null);  
            final int height = image.getHeight(null);  
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
            Graphics2D g = bufferedImage.createGraphics();  
            g.drawImage(image, 0, 0, null);  
              
            Image waterImage = ImageIO.read(new File(waterImg));//水印文件  
            int width_wi=waterImage.getWidth(null);  
            int height_wi=waterImage.getHeight(null);  
              
            if(width<=width_wi || height<=height_wi){  
                throw new Exception("原图的宽、高必须大于水印图的宽、高");  
            }  
              
           // AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha);  
              
            int widthDiff = width-width_wi;  
            int heightDiff = height-height_wi;  
              
            if(x<0){  
                x = widthDiff/2;  
            }else if(x>widthDiff){  
                x = widthDiff;  
            }  
              
            if(y<0){  
                y = heightDiff/2;  
            }else if(y>heightDiff){  
                y = heightDiff;  
            }  
            //g.drawImage(waterImage, x, y, width_wi,height_wi,null);//水印文件结束 
            //Math.toRadians(Math.tan((double)height_wi / width_wi))
            g.rotate(-150, 30,20);
            g.drawImage(waterImage, 0, 0, width,height_wi,null);
            g.dispose();  
//            ImageIO.write(bufferedImage, "png", new File(targetImg));
            return write(bufferedImage, targetImg);
        } catch (IOException e) {  
        	e.printStackTrace();
            return false;
        }  
    }  
	

	public static void makeShqp(String newFile, String code, String year, String month, String day, int sourceType){
		try {
			Image srcImg;
			if(sourceType == 2){
				srcImg = ImageIO.read(ImageUtils.class.getResource("/xiniu_merchant_auth_card.jpg"));
			} else {
				srcImg = ImageIO.read(ImageUtils.class.getResource("/xiniu_auth_card_template.jpg"));
			}

			final int srcHeight = srcImg.getHeight(null);
			final int srcWidth = srcImg.getWidth(null);
			//TYPE_BYTE_INDEXED  TYPE_USHORT_555_RGB  TYPE_USHORT_565_RGB
			BufferedImage buffImg = new BufferedImage(srcWidth,srcHeight, BufferedImage.TYPE_INT_RGB);

			// 1、得到画笔对象
			Graphics2D g = buffImg.createGraphics();
			g.drawImage(srcImg, 0, 0, srcWidth, srcHeight, null);
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1));
			g.setColor(Color.BLACK);
			g.setFont( new Font("微软雅黑", Font.PLAIN, 28));
			g.drawString("授权编号："+code,800-(code.length()-10)*16-30,170);
			g.setFont( new Font("微软雅黑", Font.PLAIN, 22));
			if(sourceType == 2){
				g.drawString(year,878,624);
				g.drawString(month,960,624);
				g.drawString(day,1004,624);
			} else {
				g.drawString(year,868,620);
				g.drawString(month,944,620);
				g.drawString(day,990,620);
			}


			// 7、释放资源
			g.dispose();

			write(buffImg,newFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 水印文字,图片输出采用JPEGImageEncoder比ImageIO.write 生成的图片文件小
	 * @param waterText
	 * @param srcImgPath
	 * @param targerPath
	 * @throws Exception
	 */
	public static boolean waterWordsAuto(String waterText, String srcImgPath,
									  String targerPath, Color color){
		try {
			Image srcImg = ImageIO.read(new File(srcImgPath));
			final int srcHeight = srcImg.getHeight(null);
			final int srcWidth = srcImg.getWidth(null);
			//TYPE_BYTE_INDEXED  TYPE_USHORT_555_RGB  TYPE_USHORT_565_RGB
			BufferedImage buffImg = new BufferedImage(srcWidth,srcHeight, BufferedImage.TYPE_INT_RGB);

			//计算对角线长度
			double diagOnal = Math.sqrt(srcHeight*srcHeight+srcWidth*srcWidth);
			int big = new BigDecimal(diagOnal/waterText.length()).intValue();
			if(big < 8)big = 8;
			// 1、得到画笔对象
			Graphics2D g = buffImg.createGraphics();
			g.drawImage(srcImg, 0, 0, srcWidth, srcHeight, null);
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,Constant.WATERMARK_ALPHA));
			g.setFont( new Font("宋体", Font.PLAIN, big));
			g.setColor(color);
			double atan = Math.atan(Double.valueOf(srcHeight)/srcWidth);
//            System.out.println("弧度:"+atan);
//            double jiaodu = Math.toDegrees(atan);
//            System.out.println("角度："+jiaodu);
			g.rotate(atan,big,big);
			g.drawString(waterText,big,big);


			// 7、释放资源
			g.dispose();

			// 8、生成图片
//			OutputStream os = new FileOutputStream(targerPath);
//
//			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
//			encoder.encode(buffImg); //近JPEG编码
//			os.close();
			write(buffImg,targerPath);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static void main(String[] args) {
//		File file = new File("d://img/180905102150387136_5.png");
//		System.out.println(file.getName().lastIndexOf("."));
//		System.out.println(file.getName().substring(file.getName().lastIndexOf(".")+1));
		
		String newFile = "d://img/delete_180905102150387136_thumbnailator.png";
		makeShqp(newFile,"code", "2019","05","30", 2);
		System.out.println("end");
	}
}
